package com.sergio.compress.model.channelWapper;

import com.sergio.compress.model.base.ChannelWrapper;
import lombok.Data;
import lombok.ToString;
import org.springframework.util.StopWatch;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author : Sergio.D
 * @Description :
 * @CreateDate ：2023/6/15 20:02
 */
@Data
public abstract class ProtoWrapper<T> extends ChannelWrapper<T> {

    @Override
    public String getChannelName() {
        return "proto";
    }

    public abstract byte[] getSendBytes();

    @Override
    public byte[] serialize() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        byte[] result = getSendBytes();
        stopWatch.stop();
        timeSe = stopWatch.getTotalTimeNanos();
        lengthSource = result.length;
        return result;
    }

    public abstract T dseSource(byte[] source);
    @Override
    public void deserialize(byte[] source) {
        try {
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            T testDataList = dseSource(source);
            stopWatch.stop();
            timeDse = stopWatch.getTotalTimeNanos();
            this.resultSe = this.dataSend.equals(testDataList);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (!this.resultSe) {
            throw new RuntimeException("反解析结果不一致!!!");
        }
    }

}
